<template>
  <div class="api-authority-pop">
    <el-button type="info" icon="el-icon-plus" size="small" @click="addBtn"
      >添加</el-button
    >
    <el-row class="list-title">
      <el-col :span="8" class="moudel">接口名称</el-col>
      <el-col :span="16" class="moudel bor">接口路径</el-col>
    </el-row>
    <el-row
      class="list-cell"
      v-for="(item, index) in api_authority"
      :key="index"
    >
      <div class="delete-btn" @click="removeBtn(item, index)">
        <i class="el-icon-remove"></i>
      </div>
      <el-col :span="8" class="moudel">
        <el-input size="mini" v-model="item.api_name"></el-input>
      </el-col>
      <el-col :span="16" class="moudel bor">
        <el-select
          v-model="item.api_path"
          multiple
          filterable
          size="mini"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in apiOptions"
            :key="item.api_name"
            :label="item.api_path"
            :value="item.api_name"
          >
          </el-option>
        </el-select>
      </el-col>
    </el-row>
    <el-row type="flex" justify="end" class="mar-t20">
      <el-button type="primary" @click="submitBtn">确定</el-button>
    </el-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      apiData: [],
      apiOptions: [
        {
          api_name: "menu-add",
          api_path: "menu/addModify",
        },
        {
          api_name: "menu-modify",
          api_path: "menu/addModify",
        },
        {
          api_name: "menu-delete",
          api_path: "menu/delete",
        },
      ],
      api_authority: [
        {
          api_name: "",
          api_path: [],
        },
      ],
    }
  },
  methods: {
    // 添加
    addBtn() {
      let obj = {
        api_name: "",
        api_path: [],
      }
      this.api_authority.push(obj)
    },
    // 删除
    removeBtn(item, index) {
      console.log(index)
      console.log(item)
      this.api_authority.splice(index, 1)
    },
    // 提交
    submitBtn() {
      if (this.api_authority.length === 0) {
        this.$message.error("请添加接口权限")
        return false
      } else {
        this.api_authority.forEach((item) => {
          if (item.api_name.replace(/\s+/g, "") === "") {
            this.$message.error("请填写接口名称")
            return false
          }
          if (item.api_path.length === 0) {
            this.$message.error("请填写接口路径")
            return false
          }
        })
        console.log(this.api_authority)
      }
    },
  },
}
</script>
<style scoped lang="scss">
$bor-coll-ddd: 1px #dddddd solid;
.api-authority-pop {
  padding: 0 20px;
  .list-title {
    border: $bor-coll-ddd;
    .moudel {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 40px;
      padding: 10px;
    }
    .bor {
      border-left: $bor-coll-ddd;
    }
  }
  .list-cell {
    border: $bor-coll-ddd;
    border-top: 0 none;
    position: relative;
    .delete-btn {
      position: absolute;
      top: 15px;
      right: -20px;
      cursor: pointer;
      .el-icon-remove {
        color: #f56c6c;
        font-size: 18px;
      }
    }
    .moudel {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 40px;
      padding: 10px;
    }
    .bor {
      border-left: $bor-coll-ddd;
    }
  }
}
</style>
